using System;
using System.Collections.Generic;
using System.Text;
using Coriander.Xmpp.Xmpp.Model.Generic;
using System.Xml;

namespace Coriander.Xmpp.Xmpp.Model
{
    /// <summary>
    /// Represents a presence stanza.
    /// </summary>
    /// <remarks>
    /// See: http://www.xmpp.org/specs/rfc3921.html#stanzas-presence 
    /// </remarks>
    public class Presence
    {
        # region * Fields, properties and ctors *

        String type, show, status;
        Int32 priority;

        /// <summary>
        /// Gets or sets the priority.
        /// </summary>
        public Int32 Priority
        {
            get { return priority; }
            set { priority = value; }
        }

        # endregion
        
        /// <summary>
        /// Parses a Presence from the supplied string.
        /// </summary>
        /// <param name="serialized"></param>
        /// <returns></returns>
        public static Presence Parse(String serialized)
        {
            if (null == serialized)
                throw new ArgumentNullException(serialized);
 
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(serialized);

            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            mgr.AddNamespace("x", "http://jabber.org/protocol/caps");

            XmlNode priorityNode = doc.SelectSingleNode("//presence/priority/text()"); 

            //
            // Proceed to parse.
            //
            Presence result = new Presence();
            result.Priority = Int32.Parse(priorityNode.Value);

            return result;
        }
    }
}
